import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:hook_up_rent/config.dart';

class Http {
  late Dio _client;
  late BuildContext context;
  static Http of(BuildContext context) {
    return Http._internal(context);
  }

  Http._internal(BuildContext context) {
    // ignore: unnecessary_null_comparison
    if (_client == null || context != this.context) {
      this.context = context;
      var options = BaseOptions(
          baseUrl: Config.baseUrl,
          connectTimeout: const Duration(seconds: 10),
          receiveTimeout: const Duration(seconds: 10),
          extra: {'context': context});
      _client = Dio(options);
    }
  }
  Future<Response<Map<String, dynamic>>> get(String path,
      [Map<String, dynamic>? params, String? token]) async {
    var options = Options(headers: {'token': token});
    return await _client.get(path, queryParameters: params, options: options);
  }

  Future<Response<Map<String, dynamic>>> post(String path,
      [Map<String, dynamic>? params, String? token]) async {
    var options = Options(headers: {'token': token});
    return await _client.post(path, data: params, options: options);
  }

  Future<Response<Map<String, dynamic>>> postFormData(String path,
      [Map<String, dynamic>? params, String? token]) async {
    var options =
        Options(contentType: 'multipart/form-data', headers: {'token': token});
    return await _client.post(path, data: params, options: options);
  }
}
